package objectio;

import java.io.*;

public class TestDriverIO {
    public static void main(String[] args) {

        System.out.println("========对象流和序列化，transient关键字======读入对象流和写入对象流==============");
        test01();
    }


    /**
     * 对象流和序列化，transient关键字
     */
    public static void test01() {

        //在内存中创建一个Teacher的实例对象
        Teacher teacher = new Teacher();
        //设值
        teacher.setTeaName("战三");
        teacher.setTeaNum("10111");
        teacher.setTeaSore(50);

        //异常处理
        try {
            //写出对象
           ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    new FileOutputStream("E:\\temp\\filesdemo\\temp100_1.dat"));

            //写出对象
            objectOutputStream.writeObject(teacher);
            System.out.println("将teacher对象从内存中写出到文件了");
            System.out.println(teacher);
            System.out.println("------------------------------------------");
            System.out.println();
            //读入对象流
            ObjectInputStream objectInputStream = new ObjectInputStream(
                    new FileInputStream("E:\\temp\\filesdemo\\temp100_1.dat"));

            //读入对象流
            Object objIn = objectInputStream.readObject();
            Teacher teacher1 = (Teacher) objIn;
            System.out.println("读入对象流：");
            System.out.println(objIn);

            //写出的teacher对象和读入对象流是同一个对象
            System.out.println("teacher == objIn :"+(teacher == objIn));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //代表所有的异常处理机制
        /*catch (Exception e) {
                    e.printStackTrace();
            }*/

    }

}
